/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.settings.standardValues;

import systole.domain.clinicalInformation.standardValues.StandardParams;
import systole.domain.clinicalInformation.standardValues.StandardValue;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.utils.ImageUtils;
import systole.view.abstractController.AbstractEditViewController;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public class ControllerStandardValues extends AbstractEditViewController {

    private JDialogStandardValues form;
    private FacadeDB facadeDB;
    private StandardValue ldl;
    private StandardValue hdl;
    private StandardValue total;
    private StandardValue triglycerides;
    private StandardValue glucemy;
    private StandardValue ps;
    private StandardValue pd;

    /**
     * 
     */
    public ControllerStandardValues() {
        super();
        this.facadeDB = FacadeDB.getInstance();
    }

    @Override
    public void ok() {
        String msg = this.valid();
        if (msg == null) {
            try {
                this.loadEntitiesFromForm();
                this.form.setVisible(false);
                this.form.dispose();
                this.save();
                this.facadeDB.commitTransaction();
            } catch (ExceptionDAO ex) {
                this.facadeDB.rollbackTransaction();
                ErrorDialog.showError(this.form, ex.getMessage());
            }
        } else {
            ErrorDialog.showError(this.form, msg);
        }
    }

    @Override
    public void cancel() {
        this.form.setVisible(false);
        this.facadeDB.rollbackTransaction();
        this.form.dispose();
    }

    private void save() throws ExceptionDAO {
        this.facadeDB.getStandardValuesBroker().update(this.ldl);
        this.facadeDB.getStandardValuesBroker().update(this.hdl);
        this.facadeDB.getStandardValuesBroker().update(this.total);
        this.facadeDB.getStandardValuesBroker().update(this.glucemy);
        this.facadeDB.getStandardValuesBroker().update(this.triglycerides);
        this.facadeDB.getStandardValuesBroker().update(this.pd);
        this.facadeDB.getStandardValuesBroker().update(this.pd);
    }

    /**
     *
     * @param parent
     */
    public void showForm(java.awt.Frame parent) {
        try {
            this.form = new JDialogStandardValues(parent, this);
            this.facadeDB.startTransaction();
            this.initFormsControls();
            this.form.setLocationRelativeTo(parent);
            this.setCloseListener(this.form);
            this.form.setVisible(true);
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            this.form.setVisible(false);
            this.form.dispose();
            ErrorDialog.showError(this.form, ex.getMessage());
        }
    }

    private void initFormsControls() throws ExceptionDAO {
        this.setCloseListener(this.form);

        this.form.setIconImage(ImageUtils.buildImage("resources/icons/default_clinical_info/thermometer32.png"));
        if (this.facadeDB.getStandardValuesBroker().getAllStandardValues().isEmpty()) {
            this.facadeDB.getStandardValuesBroker().initSatandardValues();
        }
        this.ldl = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.LDL);
        this.hdl = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.HDL);
        this.total = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TOTAL);
        this.glucemy = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.GLUCEMY);
        this.triglycerides = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.TRIGLYCERIDES);
        this.ps = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PS);
        this.pd = this.facadeDB.getStandardValuesBroker().getStandardValueByParamType(StandardParams.PD);

        this.form.getjTxtLDLNormal().setValue(this.ldl.getNormal());
        this.form.getjTxtLDLMedium().setValue(this.ldl.getMedium());
        this.form.getjTxtLDLHigh().setValue(this.ldl.getHigh());

        this.form.getjTxtHDLNormal().setValue(this.hdl.getNormal());
        this.form.getjTxtHDLMedium().setValue(this.hdl.getMedium());
        this.form.getjTxtHDLHigh().setValue(this.hdl.getHigh());

        this.form.getjTxtTotalNormal().setValue(this.total.getNormal());
        this.form.getjTxtTotalMedium().setValue(this.total.getMedium());
        this.form.getjTxtTotalHigh().setValue(this.total.getHigh());

        this.form.getjTxtGlucemyNormal().setValue(this.glucemy.getNormal());
        this.form.getjTxtGlucemyMedium().setValue(this.glucemy.getMedium());
        this.form.getjTxtGlucemyHigh().setValue(this.glucemy.getHigh());

        this.form.getjTxtTriglyceridesNormal().setValue(this.triglycerides.getNormal());
        this.form.getjTxtTriglyceridesMedium().setValue(this.triglycerides.getMedium());
        this.form.getjTxtTriglyceridesHigh().setValue(this.triglycerides.getHigh());

        this.form.getjTxtPSNormal().setValue(this.ps.getNormal());
        this.form.getjTxtPSMedium().setValue(this.ps.getMedium());
        this.form.getjTxtPSHigh().setValue(this.ps.getHigh());

        this.form.getjTxtPDNormal().setValue(this.pd.getNormal());
        this.form.getjTxtPDMedium().setValue(this.pd.getMedium());
        this.form.getjTxtPDHigh().setValue(this.pd.getHigh());
    }

    private void loadEntitiesFromForm() {

        String value;
        // load ldl normal
        try {
            value = this.form.getjTxtLDLNormal().getText();
            value = value.replace(",", ".");
            this.ldl.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load ldl medium
        try {
            value = this.form.getjTxtLDLMedium().getText();
            value = value.replace(",", ".");
            this.ldl.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load ldl high
        try {
            value = this.form.getjTxtLDLHigh().getText();
            value = value.replace(",", ".");
            this.ldl.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }


        // load hdl normal
        try {
            value = this.form.getjTxtHDLNormal().getText();
            value = value.replace(",", ".");
            this.hdl.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load hdl medium
        try {
            value = this.form.getjTxtHDLMedium().getText();
            value = value.replace(",", ".");
            this.hdl.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load hdl high
        try {
            value = this.form.getjTxtHDLHigh().getText();
            value = value.replace(",", ".");
            this.hdl.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load c total normal
        try {
            value = this.form.getjTxtTotalNormal().getText();
            value = value.replace(",", ".");
            this.total.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load c total medium
        try {
            value = this.form.getjTxtTotalMedium().getText();
            value = value.replace(",", ".");
            this.total.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load c total high
        try {
            value = this.form.getjTxtTotalHigh().getText();
            value = value.replace(",", ".");
            this.total.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }


        // load trigliceridos normal
        try {
            value = this.form.getjTxtTriglyceridesNormal().getText();
            value = value.replace(",", ".");
            this.triglycerides.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load trigliceridos medium
        try {
            value = this.form.getjTxtTriglyceridesMedium().getText();
            value = value.replace(",", ".");
            this.triglycerides.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load trigliceridos high
        try {
            value = this.form.getjTxtTriglyceridesHigh().getText();
            value = value.replace(",", ".");
            this.triglycerides.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load glucemy normal
        try {
            value = this.form.getjTxtGlucemyNormal().getText();
            value = value.replace(",", ".");
            this.glucemy.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load glucemy medium
        try {
            value = this.form.getjTxtGlucemyMedium().getText();
            value = value.replace(",", ".");
            this.glucemy.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load glucemy high
        try {
            value = this.form.getjTxtGlucemyHigh().getText();
            value = value.replace(",", ".");
            this.glucemy.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load PS normal
        try {
            value = this.form.getjTxtPSNormal().getText();
            value = value.replace(",", ".");
            this.ps.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load PS medium
        try {
            value = this.form.getjTxtPSMedium().getText();
            value = value.replace(",", ".");
            this.ps.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load PS high
        try {
            value = this.form.getjTxtPSHigh().getText();
            value = value.replace(",", ".");
            this.ps.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }


        // load PD Normal
        try {
            value = this.form.getjTxtPDNormal().getText();
            value = value.replace(",", ".");
            this.pd.setNormal(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load PD medium
        try {
            value = this.form.getjTxtPDMedium().getText();
            value = value.replace(",", ".");
            this.pd.setMedium(new Float(value));
        } catch (NumberFormatException e) {
        }

        // load PD high
        try {
            value = this.form.getjTxtPDHigh().getText();
            value = value.replace(",", ".");
            this.pd.setHigh(new Float(value));
        } catch (NumberFormatException e) {
        }

    }

    private String valid() {

        String result = this.validateLDL();
        if (result != null) {
            return result;
        }

        result = this.validateHDL();
        if (result != null) {
            return result;
        }

        result = this.validateTotal();
        if (result != null) {
            return result;
        }

        result = this.validateTriglycerides();
        if (result != null) {
            return result;
        }

        result = this.validateGlucemy();
        if (result != null) {
            return result;
        }

        result = this.validatePS();
        if (result != null) {
            return result;
        }

        result = this.validatePD();
        if (result != null) {
            return result;
        }
        return null;
    }

    private String validateLDL() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtLDLNormal().isEditValid()) {
                return "El valor normal de LDL no es válido";
            }
            value = this.form.getjTxtLDLNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de LDL no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de LDL no es válido";
        }

        try {
            if (!this.form.getjTxtLDLMedium().isEditValid()) {
                return "El valor medio-alto de LDL no es válido";
            }

            value = this.form.getjTxtLDLMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-alto de LDL no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-alto de LDL no es válido";
        }

        if (normal.floatValue() >= medium.floatValue()) {
            return "El valor medio-alto de LDL debe ser mayor que el normal";
        }

        try {
            if (!this.form.getjTxtLDLHigh().isEditValid()) {
                return "El valor alto de LDL no es válido";
            }

            value = this.form.getjTxtLDLHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor alto de LDL no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor alto de LDL no es válido";
        }

        if (medium.floatValue() >= high.floatValue()) {
            return "El valor alto de LDL debe ser mayor que el medio-alto";
        }
        return null;
    }

    private String validateHDL() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtHDLNormal().isEditValid()) {
                return "El valor normal de HDL no es válido";
            }
            value = this.form.getjTxtHDLNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de HDL no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de HDL no es válido";
        }

        try {
            if (!this.form.getjTxtHDLMedium().isEditValid()) {
                return "El valor medio-bajo de HDL no es válido";
            }

            value = this.form.getjTxtHDLMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-bajo de HDL no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-bajo de HDL no es válido";
        }

        if (normal.floatValue() <= medium.floatValue()) {
            return "El valor medio-bajo de HDL debe ser menor que el normal";
        }

        try {
            if (!this.form.getjTxtHDLHigh().isEditValid()) {
                return "El valor bajo de HDL no es válido";
            }

            value = this.form.getjTxtHDLHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor bajo de HDL no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor bajo de HDL no es válido";
        }

        if (medium.floatValue() <= high.floatValue()) {
            return "El valor bajo de HDL debe ser menor que el medio-bajo";
        }
        return null;
    }

    private String validateTotal() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtTotalNormal().isEditValid()) {
                return "El valor normal de Colesterol Total no es válido";
            }
            value = this.form.getjTxtTotalNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de Colesterol Total no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de Colesterol Total no es válido";
        }

        try {
            if (!this.form.getjTxtTotalMedium().isEditValid()) {
                return "El valor medio-alto de Colesterol Total no es válido";
            }

            value = this.form.getjTxtTotalMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-alto de Colesterol Total no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-alto de Colesterol Total no es válido";
        }

        if (normal.floatValue() >= medium.floatValue()) {
            return "El valor medio-alto de Colesterol Total debe ser mayor que el normal";
        }

        try {
            if (!this.form.getjTxtTotalHigh().isEditValid()) {
                return "El valor alto de Colesterol Total no es válido";
            }

            value = this.form.getjTxtTotalHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor alto de Colesterol Total no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor alto de Colesterol Total no es válido";
        }

        if (medium.floatValue() >= high.floatValue()) {
            return "El valor alto de Colesterol Total debe ser mayor que el medio-alto";
        }
        return null;
    }

    private String validateTriglycerides() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtTriglyceridesNormal().isEditValid()) {
                return "El valor normal de Trigliceridos no es válido";
            }
            value = this.form.getjTxtTriglyceridesNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de Trigliceridos no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de Trigliceridos no es válido";
        }

        try {
            if (!this.form.getjTxtTriglyceridesMedium().isEditValid()) {
                return "El valor medio-alto de Trigliceridos no es válido";
            }

            value = this.form.getjTxtTriglyceridesMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-alto de Trigliceridos no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-alto de Trigliceridos no es válido";
        }

        if (normal.floatValue() >= medium.floatValue()) {
            return "El valor medio-alto de Trigliceridos debe ser mayor que el normal";
        }

        try {
            if (!this.form.getjTxtTriglyceridesHigh().isEditValid()) {
                return "El valor alto de Trigliceridos no es válido";
            }

            value = this.form.getjTxtTriglyceridesHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor alto de Trigliceridos no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor alto de Trigliceridos no es válido";
        }

        if (medium.floatValue() >= high.floatValue()) {
            return "El valor alto de Trigliceridos debe ser mayor que el medio-alto";
        }
        return null;
    }

    private String validateGlucemy() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtGlucemyNormal().isEditValid()) {
                return "El valor normal de Glucemia no es válido";
            }
            value = this.form.getjTxtGlucemyNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de Glucemia no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de Glucemia no es válido";
        }

        try {
            if (!this.form.getjTxtGlucemyMedium().isEditValid()) {
                return "El valor medio-alto de Glucemia no es válido";
            }

            value = this.form.getjTxtGlucemyMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-alto de Glucemia no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-alto de Glucemia no es válido";
        }

        if (normal.floatValue() >= medium.floatValue()) {
            return "El valor medio-alto de Glucemia debe ser mayor que el normal";
        }

        try {
            if (!this.form.getjTxtGlucemyHigh().isEditValid()) {
                return "El valor alto de Glucemia no es válido";
            }

            value = this.form.getjTxtGlucemyHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor alto de Glucemia no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor alto de Glucemia no es válido";
        }

        if (medium.floatValue() >= high.floatValue()) {
            return "El valor alto de Glucemia debe ser mayor que el medio-alto";
        }
        return null;
    }

    private String validatePS() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtPSNormal().isEditValid()) {
                return "El valor normal de PS no es válido";
            }
            value = this.form.getjTxtPSNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de PS no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de PS no es válido";
        }

        try {
            if (!this.form.getjTxtPSMedium().isEditValid()) {
                return "El valor medio-alto de PS no es válido";
            }

            value = this.form.getjTxtPSMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-alto de PS no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-alto de PS no es válido";
        }

        if (normal.floatValue() >= medium.floatValue()) {
            return "El valor medio-alto de PS debe ser mayor que el normal";
        }

        try {
            if (!this.form.getjTxtPSHigh().isEditValid()) {
                return "El valor alto de PS no es válido";
            }

            value = this.form.getjTxtPSHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor alto de PS no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor alto de PS no es válido";
        }

        if (medium.floatValue() >= high.floatValue()) {
            return "El valor alto de PS debe ser mayor que el medio-alto";
        }
        return null;
    }

    private String validatePD() {
        Float normal;
        Float medium;
        Float high;
        String value;

        try {
            if (!this.form.getjTxtPDNormal().isEditValid()) {
                return "El valor normal de PD no es válido";
            }
            value = this.form.getjTxtPDNormal().getText();
            value = value.replace(",", ".");
            normal = new Float(value);
            if ((normal == null) || (!(normal.floatValue() > 0))) {
                return "El valor normal de PD no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor normal de PD no es válido";
        }

        try {
            if (!this.form.getjTxtPDMedium().isEditValid()) {
                return "El valor medio-alto de PD no es válido";
            }

            value = this.form.getjTxtPDMedium().getText();
            value = value.replace(",", ".");
            medium = new Float(value);
            if ((medium == null) || (!(medium.floatValue() > 0))) {
                return "El valor medio-alto de PD no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor medio-alto de PD no es válido";
        }

        if (normal.floatValue() >= medium.floatValue()) {
            return "El valor medio-alto de PD debe ser mayor que el normal";
        }

        try {
            if (!this.form.getjTxtPDHigh().isEditValid()) {
                return "El valor alto de PD no es válido";
            }

            value = this.form.getjTxtPDHigh().getText();
            value = value.replace(",", ".");
            high = new Float(value);
            if ((high == null) || (!(high.floatValue() > 0))) {
                return "El valor alto de PD no es válido";
            }
        } catch (NumberFormatException e) {
            return "El valor valor alto de PD no es válido";
        }

        if (medium.floatValue() >= high.floatValue()) {
            return "El valor alto de PD debe ser mayor que el medio-alto";
        }
        return null;
    }
}
